package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"life/api/request"
	"life/consts"
	"life/internal/dao"
	"life/internal/model"
	"life/pkg"
	"net/http"
	"time"
)

func Login(c *gin.Context) {
	var req request.Login
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    1000,
			"message": "未接收参数",
			"data":    nil,
		})
		return
	}

	var data model.User
	dao.GetOneInformation(&model.User{Username: req.Username, Password: req.Password}, &data)
	if data.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    1000,
			"message": "账号或密码不正确",
			"data":    nil,
		})
		return
	}
	token, err := pkg.CreateJwtToken(data.Id, time.Hour*12, consts.JWT_TOKEN_SECRET)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    1000,
			"message": "Token生成失败",
			"data":    nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "登录成功",
		"data":    token,
	})

}
